<template>
  <div id="app-container">
    <!-- 输入表单 -->
    <el-form label-width="120px">
      <el-form-item label="讲师名称">
        <el-input v-model="teacher.name" />
      </el-form-item>
      <el-form-item label="入驻时间">
        <el-date-picker v-model="teacher.joinDate" value-format="yyyy-MM-dd" />
      </el-form-item>
      <el-form-item label="讲师排序">
        <el-input-number v-model="teacher.sort" :min="0"/>
      </el-form-item>
      <el-form-item label="讲师头衔">
        <el-select v-model="teacher.level">
          <!--
            数据类型一定要和取出的json中的一致，否则没法回填
            因此，这里value使用动态绑定的值，保证其数据类型是number
            -->
          <el-option :value="1" label="高级讲师"/>
          <el-option :value="2" label="首席讲师"/>
        </el-select>
      </el-form-item>
      <el-form-item label="讲师简介">
        <el-input v-model="teacher.intro"/>
      </el-form-item>
      <el-form-item label="讲师资历">
        <el-input v-model="teacher.career" :rows="10" type="textarea"/>
      </el-form-item>
      <!-- 讲师头像：TODO -->
      <el-form-item label="讲师头像">
        <el-upload
          :show-file-list="false"
          :on-error="handleAvatarError"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          class="avatar-uploader"
          action="http://localhost:8120/admin/oss/file/upload?module=avatar">
          <img v-if="teacher.avatar" :src="teacher.avatar" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"/>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate()">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import teacherApi from '@/api/teacher'
export default {
  data() {
    return {
      teacher: {},
      saveBtnDisabled: false
    }
  },
  created() {
    // console.log('获取到的id' + this.$route.params.id)
    // 获取id对应的讲师信息回显
    // 只有更新时才需要跳转到此组件执行查询方法
    if (this.$route.params.id) {
      this.getTeacher()
    }
  },
  methods: {
    // 更新讲师
    update() {
      teacherApi.teacherUpdate(this.teacher)
        .then(response => {
          this.$message.success({
            message: `《${this.teacher.name}》讲师更新成功`,
            type: 'success'
          })
          this.$router.push('/teacher')
        })
    },
    // 注册讲师
    save() {
      teacherApi.teacherSave(this.teacher)
        .then(response => {
          this.$message.success({
            message: `《${this.teacher.name}》讲师注册成功`,
            type: 'success'
          })
          // 路由到讲师列表页面 参考：VueRoutr Api
          // $router：代表整个vue的所有路由集合配置对象，对应router目录
          // $route：代表当前form.vue对应的路由对象
          this.$router.push('/teacher') // push中推入路由已存在的地址，vue会自动路由到对应的页面显示
        })
    },
    // 更新和注册
    saveOrUpdate() {
      // 禁用注册按钮
      this.saveBtnDisabled = true
      if (this.teacher.id) {
        // id存在证明是更新操作
        this.update()
      } else {
        // id不存在证明是注册操作
        this.save()
      }
    },
    // 获取id对应的讲师信息
    getTeacher() {
      teacherApi.teacherGetById(this.$route.params.id)
        .then(response => {
          // 通过response获取到后端teacher对象在设置给前端的teacher
          this.teacher = response.data.item
        })
    },
    // 上传头像回调方法
    handleAvatarSuccess(response, file) {
      if (response.code === 20000) {
        // 上传成功
        this.teacher.avatar = response.data.path
        // 把上传的头像强制渲染到页面
        this.$forceUpdate()
      } else {
        // 上传失败提示
        this.$message.success({
          message: '头像上传失败',
          type: 'error' })
      }
    },
    // 上传头像之前校验
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 服务器断开连接上传头像失败显示
    handleAvatarError() {
      this.$message.success({
        message: '服务器连接失败',
        type: 'error' })
    }
  }
}
</script>

// 上传头像框的样式
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
